package cn.damai.mine.json.fastjson;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class JsonDemo {

	public static void main(String[] args) {
		
		final String index = "234579AF-028D-485C-B208-750AB10AA6A7";
		System.out.println(index.toLowerCase());
		
		User u = new User();
		dealWithUser(u);
		System.out.println(u.getId());
		
		User uu = null;
		System.out.println(JSONObject.toJSONString(uu));
		
		
		/*json2JsonObjectM1();
		json2BeanM2();
		jsonWithJSONField();*/
		/*complexMap2JsonM9();
		complex2JsonM8();
		complexExampleM7();
		bean2JSONObjectM6();
		jsonWithDate();
		*/
		dealWithObj();
		
	}
	
	private static void  json2JsonObjectM1(){
		
		System.out.println("===========JsonText to JsonObject=============");
		
		String s = "{\"id\":\"0375\",\"city\":\"北京\"}";
		
		JSONObject obj = JSONObject.parseObject(s);
		System.out.println(obj.get("city"));
		
		JSONObject obj2 = JSON.parseObject(s);
		System.out.println(obj2.get("city"));
	}
	
	private static void json2BeanM2(){
		
		System.out.println("===========JsonText to JavaBean=============");
		
		String s = "{\"id\":\"0375\",\"city\":\"北京\"}";
		
		Weibo weibo = JSON.parseObject(s, Weibo.class);
		System.out.println(weibo.getId()+weibo.getCity());
		
		
		Weibo weibo2 = JSONObject.parseObject(s, Weibo.class);
		System.out.println(weibo2.getId()+weibo2.getCity());
		
	}
	
	private static void jsonWithJSONField(){
		
		System.out.println("===========jsonWithJSONField=============");
		
		String s = "{\"id\":\"0375\",\"city\":\"北京\",\"otherId\":\"10020\"}";
		
		Weibo weibo = JSON.parseObject(s, Weibo.class);
		System.out.println(weibo.getId()+weibo.getCity()+weibo.getName());
		
		
		Weibo weibo2 = JSONObject.parseObject(s, Weibo.class);
		System.out.println(weibo2.getId()+weibo2.getCity()+weibo.getName());
		
	}
	
	private static void complexMap2JsonM9(){
		
		System.out.println("===========Map to Json=============");
		
		Group group = new Group();
		group.setId(1);
		group.setName("group");
		
		User user1 = new User();
        user1.setId(2);
        user1.setName("user1");
  
        User user2 = new User();
        user2.setId(3);
        user2.setName("user2");
        
        group.getList().add(user1);
        group.getList().add(user2);
        
    	String s = "{\"id\":\"0375\",\"city\":\"北京\"}";
		Weibo weibo = JSON.parseObject(s, Weibo.class);
        
        
        Map<Integer,Object> map = new HashMap<Integer,Object>();
        map.put(1, "No.1");
        map.put(2, "No.2");
        map.put(3, weibo);
        map.put(4, group.getList());
        
        String jsonStr = JSON.toJSONString(map);
        System.out.println(jsonStr);
        
	}
	
	public static void complex2JsonM8(){
		
		System.out.println("===========Object to JsonText=============");
		
		Group group = new Group();
		group.setId(1);
		group.setName("group");
		
		User user1 = new User();
        user1.setId(2);
        user1.setName("user1");
  
        User user2 = new User();
        user2.setId(3);
        user2.setName("user2");
        
        group.getList().add(user1);
        group.getList().add(user2);
        
        String jsonStr = JSON.toJSONString(group);
        System.out.println(jsonStr);
	}
	
	public static void complexExampleM7(){
		
		System.out.println("===========JsonText to JsonArr=============");
		
		String s = "{js:[{id:\"110000\",\"city\":\"北京000\"},{id:\"110001\",\"city\":\"上海001\"}"
                + ",{id:\"110002\",\"city\":\"南京002\"},{id:\"110003\",\"city\":\"杭州003\"},"
                + "{id:\"110006\",\"city\":\"青岛007\"},{id:\"120007\",\"city\":\"其他地区009\"}]}";
		
		JSONObject json = JSON.parseObject(s);
		Object jsonArr = json.get("js");
		System.out.println(jsonArr);
		
		List<Weibo> list = JSON.parseArray(jsonArr.toString(), Weibo.class);
		for (Weibo weibo : list) {
			System.out.println(weibo.getCity());
		}
		
		System.out.println("************the second mode**********");
		
		JSONArray array = JSON.parseArray(jsonArr.toString());
		for (Object object : array) {
			Weibo wo = JSON.parseObject(object.toString(), Weibo.class);
			System.out.println(wo.getCity());
		}
		
		System.out.println("************the third mode**********");
		
		JSONArray array2 = JSON.parseArray(jsonArr.toString());
		String str = String.valueOf(array2.get(1));
		
		System.out.println(array2.get(0));
		JSONObject object = JSON.parseObject(str);
		System.out.println(object.get("id"));
		
	}
	
	private static void bean2JSONObjectM6(){
		
		System.out.println("===========JavaBean to JsonObject=============");
		
		Weibo weibo = new Weibo("0373", "你的名字");
		JSONObject obj = (JSONObject) JSON.toJSON(weibo);
		System.out.println(obj.get("id"));
		
		System.out.println("************lalalala lalala**********");
		
		String string = JSON.toJSONString(weibo);
        System.out.println(string);
	}
	
	private static void jsonWithDate(){
		
		System.out.println("===========Json with Date=============");
		
		long millis = System.currentTimeMillis();
		Date date =new Date(millis);
		
		System.out.println(JSON.toJSONString(date));
		
		System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat));
		
		System.out.println(JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd HH:mm:ss.SSS"));
	}
	
	private static void dealWithUser(User u){
		u.setId(11);
		u.setName("haha");
	}
	
	private static void dealWithObj(){
		
		Group group = new Group();
		group.setId(1);
		group.setName("group");
		
		User user1 = new User();
        user1.setId(2);
        user1.setName("user1");
  
        User user2 = new User();
        user2.setId(3);
        user2.setName("user2");
        
        group.getList().add(user1);
        group.getList().add(user2);
        
        System.out.println(JSON.toJSON(group));
		
	}
	
}
